import 'dart:ui';

import 'package:flutter_elf/config/component_index.dart';

import 'NovelReaderViewModel.dart';
import 'manager_menu_widget.dart';
import 'manager_reader_page.dart';
import 'widget_reader_painter.dart';

class BookPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return BookPageState();
  }
}

class BookPageState extends State<BookPage>{

  ReaderPageManager pageManager;
  NovelPagePainter mPainter;

  TouchEvent currentTouchEvent = TouchEvent(TouchEvent.ACTION_UP, null);
  AnimationController animationController;

  GlobalKey canvasKey = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    NovelReaderViewModel viewModel;
    // TODO: implement build
    return Scaffold(
      body: Container(
        color: Colors.red,
        height: double.infinity,
        width: double.infinity,
        child: RawGestureDetector(
          gestures: <Type, GestureRecognizerFactory>{
            NovelPagePanGestureRecognizer:
            GestureRecognizerFactoryWithHandlers<
                NovelPagePanGestureRecognizer>(
                  () => NovelPagePanGestureRecognizer(false),
                  (NovelPagePanGestureRecognizer instance) {
                instance.setMenuOpen(false);
                instance
                  ..onDown = (detail) {
                    if (true) {
                      if (currentTouchEvent.action !=
                          TouchEvent.ACTION_DOWN ||
                          currentTouchEvent.touchPos !=
                              detail.localPosition) {
                        currentTouchEvent = TouchEvent(
                            TouchEvent.ACTION_DOWN, detail.localPosition);
                        mPainter.setCurrentTouchEvent(currentTouchEvent);
                        canvasKey.currentContext
                            .findRenderObject()
                            .markNeedsPaint();
                      }
                    }
                  };
                instance
                  ..onUpdate = (detail) {
                    if (!viewModel.getMenuOpenState()) {
                      if (currentTouchEvent.action !=
                          TouchEvent.ACTION_MOVE ||
                          currentTouchEvent.touchPos !=
                              detail.localPosition) {
                        currentTouchEvent = TouchEvent(
                            TouchEvent.ACTION_MOVE, detail.localPosition);
                        mPainter.setCurrentTouchEvent(currentTouchEvent);
                        canvasKey.currentContext
                            .findRenderObject()
                            .markNeedsPaint();
                      }
                    }
                  };
                instance
                  ..onEnd = (detail) {
                    if (!viewModel.getMenuOpenState()) {
                      if (currentTouchEvent.action != TouchEvent.ACTION_UP ||
                          currentTouchEvent.touchPos != Offset(0, 0)) {
                        currentTouchEvent = TouchEvent<DragEndDetails>(
                            TouchEvent.ACTION_UP, Offset(0, 0));
                        currentTouchEvent.touchDetail = detail;

                        mPainter.setCurrentTouchEvent(currentTouchEvent);
                        canvasKey.currentContext
                            .findRenderObject()
                            .markNeedsPaint();
                      }
                    }
                  };
              },
            ),
          },
          child: CustomPaint(
            key: canvasKey,
            isComplex: true,
            size: Size(window.physicalSize.width, window.physicalSize.width),
            painter: mPainter,
          ),
        ),
      ),
    );
  }
}